package com.aloda.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.constant.ErrorCode;
import com.aloda.constant.ExceptionConstant;
import com.aloda.context.UserBaseContext;
import com.aloda.entity.App;
import com.aloda.entity.UserCollect;
import com.aloda.enums.ReviewStatusEnum;
import com.aloda.exception.BaseException;
import com.aloda.exception.WarnException;
import com.aloda.mapper.AppMapper;
import com.aloda.mapper.CollectMapper;
import com.aloda.mapper.UserMapper;
import com.aloda.service.CollectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 用户收藏service
 *
 * @author aloda
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, UserCollect> implements CollectService {

    @Resource
    private AppMapper appMapper;

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 收藏应用
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Boolean collectApp(Long id) {
        // 1.参数校验
        checkApp(id);
        // 2.查询是否已经收藏 后期解决幂等性问题
        Long userId = UserBaseContext.getUser().getId();
        LambdaQueryWrapper<UserCollect> userCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userCollectLambdaQueryWrapper.eq(UserCollect::getAppId, id)
                .eq(UserCollect::getUserId, userId);
        Long selectCount = collectMapper.selectCount(userCollectLambdaQueryWrapper);
        if (selectCount > 0) {
            throw new BaseException(ExceptionConstant.COLLECT_IS_EXIST);
        }
        // 3.增加收藏记录
        UserCollect userCollect = new UserCollect();
        userCollect.setUserId(userId);
        userCollect.setAppId(id);
        userCollect.setCreateTime(LocalDateTime.now());
        userMapper.updateCollectIncrease(userId);
        int insert = collectMapper.insert(userCollect);
        return insert > 0;
    }

    /**
     * 取消收藏应用
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Boolean unCollectApp(Long id) {
        // 1.参数校验
        checkApp(id);
        // 2.取消收藏
        Long userId = UserBaseContext.getUser().getId();
        LambdaQueryWrapper<UserCollect> userCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userCollectLambdaQueryWrapper.eq(UserCollect::getAppId, id)
                .eq(UserCollect::getUserId, userId);
        userMapper.updateCollectDecrease(userId);
        int delete = collectMapper.delete(userCollectLambdaQueryWrapper);
        return delete > 0;
    }

    private App checkApp(Long appId) {
        if (appId == null) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        App app = appMapper.selectById(appId);
        if (BeanUtil.isEmpty(app)) {
            throw new BaseException(ExceptionConstant.APP_NOT_EXIST);
        }
        if (!app.getReviewStatus().equals(ReviewStatusEnum.PASS.getValue())) {
            throw new BaseException(ExceptionConstant.APP_NOT_REVIEW);
        }
        return app;
    }
}
